namespace SubSonic
{
    using System;
    using System.Data;

    [Serializable]
    public class ActiveList<ItemType, ListType> : AbstractList<ItemType, ListType> where ItemType: ActiveRecord<ItemType>, new() where ListType: AbstractList<ItemType, ListType>, new()
    {
        public ActiveList()
        {
        }

        public ActiveList(IDataReader rdr)
        {
            base.Load(rdr);
        }

        public ActiveList(IDataReader rdr, bool closeReader)
        {
            base.Load(rdr);
            if (closeReader)
            {
                rdr.Close();
            }
        }

        public void BatchSave()
        {
            this.BatchSave(string.Empty);
        }

        public void BatchSave(Guid userID)
        {
            this.BatchSave(userID.ToString());
        }

        public void BatchSave(int userID)
        {
            this.BatchSave(userID.ToString());
        }

        public void BatchSave(string userName)
        {
            QueryCommandCollection commands = new QueryCommandCollection();
            foreach (ItemType local in this)
            {
                QueryCommand saveCommand = local.GetSaveCommand(userName);
                if (saveCommand != null)
                {
                    commands.Add(saveCommand);
                }
            }
            DataService.ExecuteTransaction(commands);
        }

        public ListType Clone()
        {
            ListType local = Activator.CreateInstance<ListType>();
            foreach (ItemType local2 in this)
            {
                ItemType item = local2.Clone();
                local.Add(item);
            }
            return local;
        }

        public ActiveRecord<ItemType> Find(object primaryKeyValue)
        {
            ActiveRecord<ItemType> record = null;
            foreach (ItemType local in this)
            {
                if (local.GetPrimaryKeyValue().Equals(primaryKeyValue))
                {
                    record = local;
                }
            }
            return record;
        }

        public void SaveAll()
        {
            foreach (ItemType local in this)
            {
                local.Save();
            }
        }

        public void SaveAll(Guid userID)
        {
            foreach (ItemType local in this)
            {
                local.Save(userID);
            }
        }

        public void SaveAll(int userId)
        {
            foreach (ItemType local in this)
            {
                local.Save(userId);
            }
        }

        public void SaveAll(string userName)
        {
            foreach (ItemType local in this)
            {
                local.Save(userName);
            }
        }
    }
}

